import 'package:flutter/cupertino.dart';
import 'package:flutter_graphics_view/src/context/graphics_view_context.dart';
import 'package:flutter_graphics_view/src/context/scene_model_context.dart';
import 'package:flutter_graphics_view/src/context/scene_state_context.dart';
import 'package:flutter_graphics_view/src/widget/graphics_view_widget.dart';
import 'package:provider/provider.dart';

class FlutterGraphicsViewWidget extends StatefulWidget {
  final GraphicsViewContext graphicsViewContext;

  const FlutterGraphicsViewWidget(
      {super.key, required this.graphicsViewContext});

  @override
  _FlutterGraphicsViewWidgetState createState() {
    return _FlutterGraphicsViewWidgetState();
  }
}

class _FlutterGraphicsViewWidgetState extends State<FlutterGraphicsViewWidget> {
  @override
  void initState() {
    if (!widget.graphicsViewContext.sceneStateContext.initialized) {
      widget.graphicsViewContext.policySet.initializeGraphicsView();
      widget.graphicsViewContext.sceneStateContext.initialized = true;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<SceneModelContext>.value(
            value: widget.graphicsViewContext.sceneModelContext),
        ChangeNotifierProvider<SceneStateContext>.value(
            value: widget.graphicsViewContext.sceneStateContext),
      ],
      builder: (context, child) {
        return GraphicsViewWidget(
          policy: widget.graphicsViewContext.policySet,
        );
      },
    );
  }
}
